// pages/payAddress/payAddress.js
// pages/address/address.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userId: 22,
        userInfo:{},
        addressList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this=this
        const userinfo = wx.getStorageSync("userInfo");
        this.setData({
          userInfo: userinfo
        });
        console.log(_this.data.userInfo.userId)
        var uid = _this.data.userInfo.userId;
        let temp = [];
        //网络请求获取数据
        var _this = this;
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.request({
            url: 'http://43.139.97.111:8080/address/queryByUserId?userId='+uid,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                console.log(res.data);
                if (res.statusCode == 200 && res.data.data != '') {
                    _this.setData({
                        addressList: res.data.data,
                    })
                }
                wx.hideLoading()
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var _this=this
        const userinfo = wx.getStorageSync("userInfo");
        this.setData({
          userInfo: userinfo
        });
        console.log(_this.data.userInfo.userId)
        let addressList = wx.getStorageSync('address') || [];
        if (addressList.length != 0) {
            addressList[0].state = true;
        }
        this.setData({
            addressList: addressList
        })
    },
    /**默认地址被修改 */
    radioChange(e) {
        let addr = this.data.addressList;
        for (let i = 0; i < this.data.addressList.length; i++) {
            addr[i].statu = false;
        }
        addr[e.detail.value].statu = true;
        wx.setStorageSync('address', addr)
    },

    backPay(e) {
        var _this = this;
        const idx = e.currentTarget.dataset.index;
        console.log(_this.data.addressList[idx].name)
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
            address: _this.data.addressList[idx]
        })
        wx.navigateBack({
            delta: 1,
            success: (res) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
})
